package com.applancer.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.applancer.dataprovider.DataType.Type;
import com.applancer.model.FeedModel;
import com.applancer.model.GeneralModel;

public class HomePaser extends GeneralParser {

	protected GeneralParser feedPaser = GeneralParser.createInstance(Type.Post);

	@Override
	protected void parseArray(List<GeneralModel> ldata, JSONArray arr) {
		for (int i = 0; i < arr.length(); i++) {

			JSONObject obj;
			try {
				obj = arr.getJSONObject(i);
			} catch (Exception e) {
				continue;
			}
			List<GeneralModel> model = parseModelArray(obj);
			if (model != null)
				ldata.addAll(model);
		}
	}

	@Override
	public List<GeneralModel> parseModelArray(JSONObject obj) {

		List<GeneralModel> lmodel = new ArrayList<GeneralModel>();
		String catName;
		try {
			catName = obj.getString("Name");
		} catch (Exception e) {
			catName = "";
		}

		try {
			JSONArray jsonarr = obj.getJSONArray("Posts");
			feedPaser.parseArray(lmodel, jsonarr);

		} catch (Exception e) {
		}

		for (int i = 0; i < lmodel.size(); i++) {
			((FeedModel) lmodel.get(i)).setCategory(catName);
		}

		return lmodel;
	}

}
